package com.portalEsporteClubeBahia.app.bo;

import java.util.List;

import com.portalEsporteClubeBahia.app.ApplicationException;
import com.portalEsporteClubeBahia.app.dao.NewsDAO;
import com.portalEsporteClubeBahia.app.vo.NewsVO;
import com.portalEsporteClubeBahia.app.vo.TypeNewsVO;

public class NewsBO {
	
	private NewsDAO newsDAO;
	
	public NewsBO() throws ApplicationException{
		try {
			newsDAO = new NewsDAO();
		} catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public List<NewsVO> findAll() throws ApplicationException{
		try{
			return newsDAO.findAll();
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public List<NewsVO> findByFilter(NewsVO filter) throws ApplicationException{
		try{
			return newsDAO.findByFilter(filter);
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public NewsVO findById(NewsVO filter) throws ApplicationException{
		try{
			
			if(filter.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			
			return newsDAO.findById(filter);
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public List<TypeNewsVO> findByFilter(TypeNewsVO filter) throws ApplicationException{
		try{
			return newsDAO.findByFilter(filter);
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public TypeNewsVO findById(TypeNewsVO filter) throws ApplicationException{
		try{
			
			if(filter.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			
			return newsDAO.findById(filter);
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public void updateRanking(NewsVO news) throws ApplicationException{
		try{
			
			if(news.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			
			newsDAO.updateRanking(news);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public void delete(NewsVO news) throws ApplicationException{
		try{
			
			if(news.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			
			newsDAO.delete(news);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public void delete(TypeNewsVO type) throws ApplicationException{
		try{
			
			if(type.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			
			newsDAO.delete(type);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public List<TypeNewsVO> findAllTypeNews() throws ApplicationException{
		try{
			return newsDAO.findAllTypeNews();
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public void update(NewsVO news) throws ApplicationException{
		try{
			
			if(news.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			if(news.getTitle()==null || news.getTitle().equals("")){
				throw new ApplicationException("Missing Title.");
			}
			if(news.getTypeNews()==null || news.getTypeNews().getId()==0){
				throw new ApplicationException("Missing Type.");
			}
			if(news.getContent()==null || news.getContent().equals("")){
				throw new ApplicationException("Missing Content.");
			}
			if(news.getShortContent()==null || news.getShortContent().equals("")){
				throw new ApplicationException("Missing Short Content.");
			}
			if(news.getDate()==null){
				throw new ApplicationException("Missing Date.");
			}
			if(news.getUrlImage()==null || news.getUrlImage().equals("")){
				throw new ApplicationException("Missing Url Image.");
			}
			
			newsDAO.update(news);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public void insert(NewsVO news) throws ApplicationException{
		try{
			
			if(news.getTitle()==null || news.getTitle().equals("")){
				throw new ApplicationException("Missing Title.");
			}
			if(news.getTypeNews()==null || news.getTypeNews().getId()==0){
				throw new ApplicationException("Missing Type.");
			}
			if(news.getContent()==null || news.getContent().equals("")){
				throw new ApplicationException("Missing Content.");
			}
			if(news.getShortContent()==null || news.getShortContent().equals("")){
				throw new ApplicationException("Missing Short Content.");
			}
			if(news.getDate()==null){
				throw new ApplicationException("Missing Date.");
			}
			if(news.getUrlImage()==null || news.getUrlImage().equals("")){
				throw new ApplicationException("Missing Url Image.");
			}
			
			newsDAO.insert(news);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public void update(TypeNewsVO type) throws ApplicationException{
		try{
			
			if(type.getId()==null){
				throw new ApplicationException("Missing id.");
			}
			if(type.getName()==null || type.getName().equals("")){
				throw new ApplicationException("Missing Title.");
			}
			
			newsDAO.update(type);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}
	
	public void insert(TypeNewsVO type) throws ApplicationException{
		try{
			
			if(type.getName()==null || type.getName().equals("")){
				throw new ApplicationException("Missing Name.");
			}
			
			newsDAO.insert(type);
			
		}catch (ApplicationException e) {
			throw new ApplicationException(e);
		}
	}

}
